package com.sprocomm.agingtest.receiver;

import com.sprocomm.agingtest.BaseActivity;
import com.sprocomm.agingtest.Config;
import com.sprocomm.agingtest.R;
import com.sprocomm.agingtest.Tools;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ReceiverTest2 extends BaseActivity {
    private TextView tvTime;
    private Button btnStart;
    private MediaPlayer mediaPlayer;
    private AudioManager audioManager;
    //添加喇叭
    private boolean useRevicer = false;

    private boolean RESULT = false;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mediaPlayer.start();
        }
    };
    private CountDownTimer countDownTimer = new CountDownTimer(Config.RECEIVER_TIME_PASS,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvTime.setText("Remaining time\n" + Tools.longTime2String(millisUntilFinished));
        }

        @Override
        public void onFinish() {
            btnStart.setEnabled(true);
            mediaPlayer.stop();
            tvTime.setText("PASS");
            setPassBtnEnable(true);
            handler.removeCallbacks(runnable);
        }
    };
    public void start(View view){
        handler.post(runnable);
        countDownTimer.start();
        btnStart.setEnabled(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiver_test);

        tvTime = (TextView) findViewById(R.id.tvTime);
        btnStart = (Button) findViewById(R.id.btn_start);
        //添加喇叭
        Intent intent = getIntent();
        String soundUnit = intent.getStringExtra("SoundUnit");
        Tools.logE("发声单元："+soundUnit);
        if ("Receiver".equals(soundUnit)){
            useRevicer = true;
        }
//        addResultBtn();
        initMedia();
    }

    @Override
    protected void onDestroy() {
        Tools.logE("onDestroy");
        if (mediaPlayer != null){
            mediaPlayer.release();
        }
        if (audioManager != null) {
            audioManager.setSpeakerphoneOn(true);
            audioManager.setMode(AudioManager.MODE_NORMAL);
        }
        handler.removeCallbacks(runnable);
        super.onDestroy();
    }
    private void initMedia() {
        //权限<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //add SPK
        if (useRevicer){
            audioManager.setSpeakerphoneOn(false);
            //听筒播放需要使用这个MODE_IN_COMMUNICATION
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        }else {
            audioManager.setSpeakerphoneOn(true);
        }

        //如果使用create就不要prepare要不然会包status 8 的错误
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.test_music);
        mediaPlayer.setLooping(false);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Tools.logE("once 播放了一次");
                //Toast.makeText(getApplicationContext(),"bofang yici ",Toast.LENGTH_SHORT).show();
                if (mediaPlayer != null && !RESULT){
                    handler.removeCallbacks(runnable);
                    handler.post(runnable);
                }
            }
        });
        audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager
                .getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager
                .getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0);
        audioManager.setStreamVolume(AudioManager.STREAM_DTMF, audioManager
                .getStreamMaxVolume(AudioManager.STREAM_DTMF), 0);
        audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, audioManager
                .getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
        audioManager.setStreamVolume(AudioManager.STREAM_RING, audioManager
                .getStreamMaxVolume(AudioManager.STREAM_RING), 0);
        audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, audioManager
                .getStreamMaxVolume(AudioManager.STREAM_SYSTEM), 0);
    }
}
